package cn.imu.appdevexperiment2020.work01;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import cn.imu.appdevexperiment2020.MainActivity;
import cn.imu.appdevexperiment2020.R;
import cn.imu.appdevexperiment2020.base.BaseActivity;
import cn.imu.appdevexperiment2020.utils.ActivityUtil;

public class FirstActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        //获取上一个窗口的内容显示到文本狂
        //Intent intent = getIntent();

        //获取上一个窗口的bundle 要判断是否为null
        Bundle bundle = this.getIntent().getExtras();

        if (bundle != null) {
            String name = bundle.getString("name");
            int age = bundle.getInt("age");
            TextView showTest = (TextView) findViewById(R.id.show);
            showTest.setText(String.format("姓名：%s 年龄：%d", name, age));
        }


        //关闭所以窗口
        Button button = findViewById(R.id.finish);
        button.setOnClickListener((view -> {
            ActivityUtil.getInstance().finishAll();
        }));


        //回传数据
        Button backData = findViewById(R.id.huichuanshuju);
        backData.setOnClickListener((view -> {
            Intent intent = new Intent(this, MainActivity.class);
            intent.putExtra("name", "第二个窗口的数据");
            setResult(1, intent);
            finish();
        }));


    }
}